<template>
  <view>
    <block v-for="(item,index) in maintenanceOverlist" :key="index">
      <!--组件-报修列表-->
    <maintenance_list :mainteanceList="item"></maintenance_list>
   </block>
      <!--底部-->
        <myBottom :mybottom="maintenanceOverlist"></myBottom>
      <!-- 输入框示例 -->
      <uni-popup ref="inputDialog" type="dialog" :show='popshow'>
        <uni-popup-dialog v-if="isDialog" ref="inputClose" mode="input" title="拒绝理由" placeholder="请输入拒绝理由"
          @confirm="dialogInputConfirm">
          <view>
          <uni-easyinput type="text"
                       v-model="descs"
                       :placeholder="placeholder"/>
                 </view>
                 </uni-popup-dialog>
      </uni-popup>
  </view>
</template>

<script>
  import {
    queryStateToList,
    updateTask,
    addTaskState,
    sendScheduleMessage,
    delRepairImgByCode
  } from "@/api/system/task/task.js"
  import getDateTime from '../../utils/getdateTime.js'
  import myBottom from '../components/my-bottom/my-bottom.vue';
  import maintenance_list from '../components/maintenance-list/maintenance-list.vue';
  import {
    getUserItme
  } from "@/api/system/user"
  export default {
    components: {
      myBottom,
      maintenance_list
    },
    data() {
      return {
        //显示弹出框
        isDialog: false,
        descs:"",
        // 是否正在请求数据,节流阀
        isloading: false,
        maintenanceCode: null,
        maintainerId: null,
        // 查询参数
        count: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 6,
          warrantyPersonId: "",
          maintenanceStatus: 2,
        },
        maintenanceOverlist: [],
      };
    },
    //页面创建时调用数据刷新
    created() {
      uni.$on("toOverList", (data) => {
        this.maintenanceOverlist = []
        this.queryParams.pageNum = data.value
        this.queryMyToList()
      });
    },
    //防止页面重复调用
    onUnload() {
      // 移除监听事件
      uni.$off("toOverList");
    },
    onLoad(option) {
      this.queryParams.warrantyPersonId = option.userid
      this.queryMyToList()
    },
    methods: {
      queryMyToList(cb) {
        // 打开遮罩层
        this.$modal.loading("数据加载中...");
        // ** 打开节流阀
        this.isloading = true
        queryStateToList(this.queryParams).then(response => {
          if (response.code === 200) {
            // 关闭遮罩层
            this.$modal.closeLoading();
            uni.stopPullDownRefresh()
            // 关闭节流阀
            this.isloading = false
            // 只要数据请求完毕，就立即按需调用 cb 回调函数
            cb && cb()
            this.maintenanceOverlist = [...this.maintenanceOverlist, ...response.rows]
            this.count = response.total
          }
        })
      },
      empty() {
        //重置关键数据
        this.count = 0
        this.isloading = false
        this.maintenanceOverlist = []
        this.queryParams.pageNum = 1
      },
      dialogInputConfirm(val) {
        const statedata = {
          maintenanceCode: this.maintenanceCode,
          state: "用户已拒绝",
          datetime: getDateTime.dateTimeStr('y-m-d h:i:s'),
          descs: "拒绝原因: " + this.descs
        }
        const data = {
          maintenanceCode: this.maintenanceCode,
          maintenanceStatus: 3,
          repairDescription:""
        }
        addTaskState(statedata).then(response => {
          if (response.code === 200) {
            updateTask(data).then(response => {
              if (response.code === 200) {
                delRepairImgByCode(this.maintenanceCode).then(response => {
                  if (response.code == 200) {
                   getUserItme(this.maintainerId).then(response => {
                     if (response.code == 200 && response.data.openId!=null) {
                       //新增成功调用接口
                       const messagedata = {
                         openid: response.data.openId,
                         selfPage: '/pages/login',
                         maintenanceCode: this.maintenanceCode,
                         maintenanceStatus: "用户已拒绝",
                         description: "用户已拒绝,请尽快去查看原因",
                       }
                       sendScheduleMessage(messagedata).then(response => {
                         // 关闭窗口后，恢复默认内容
                         this.$refs.inputDialog.close()
                         this.empty()
                         this.queryMyToList()
                         if (response.errcode == 0) {
                           this.$modal.msgSuccess("操作成功")
                         } else {
                           this.$modal.msgError("订阅失败");
                         }
                       })
                     }else{
                       this.$refs.inputDialog.close()
                       this.empty()
                       this.queryMyToList()
                       // 关闭窗口后，恢复默认内容
                       this.$modal.msgSuccess("操作成功")
                     }
                   })
                  }
                })
              }
            })
          }
        });
      },
    },
    // 触底的事件
    onReachBottom() {
      // 判断是否还有下一页数据
      if (this.queryParams.pageNum * this.queryParams.pageSize >= this.count) return uni.$showMsg('数据加载完毕')
      // 判断是否正在请求其它数据，如果是，则不发起额外的请求
      if (this.isloading) return
      //让页码值自增+1
      this.queryParams.pageNum += 1
      // 重新获取列表数据
      this.queryMyToList()

    },
    //下拉事件
    onPullDownRefresh() {
      //重置关键数据
      this.count = 0
      this.empty()
      //重新发起数据请求
      this.queryMyToList()
    },
  }
</script>

<style lang="scss">

</style>